//
//  Agilent34410A.m
//  DMMCalibration
//
//  Created by apple on 17/2/25.
//  Copyright © 2017年 microtest.org. All rights reserved.
//

#import "Agilent34410A.h"

@implementation Agilent34410A

-(BOOL) OpenDevice
{
    _opened = NO;
    BOOL isFind = [self Find];
    
    if (!isFind)
    {
        return NO;
    }
    status = viOpen (defaultRM, instrDescriptor, VI_NULL, VI_NULL, &instr);
    
    if (status < VI_SUCCESS)
    {
        return NO;
    }
    
    [self sendCommand:@"*CLS"];
    
    _opened = YES;
    
    return YES;
    
}

-(void)SetMode:(NSString*)mode
{
    if ([mode isEqualToString:@"Fetch"])
    {
        [self sendCommand:@"*RST"];
        [self sendCommand:@"*CLS"];
        [self sendCommand:@"ABORT"];
        [self sendCommand:@":SENS:FUNC 'VOLT:DC'"];
        [self sendCommand:@":SENSe:VOLT:DC:ZERO:AUTO OFF"];
        [self sendCommand:@":SENSe:VOLT:DC:RANGE:AUTO OFF"];
        [self sendCommand:@":SENS:VOLT:DC:NPLC 0.006"]; //shorter has faster speed, longer has better resolution
        [self sendCommand:@":SENS:VOLT:DC:RANG 1"];
        [self sendCommand:@":SAMP:COUN 5000"];
        [self sendCommand:@":TRIG:DEL 0.001"];
        [self sendCommand:@":TRIG:SOUR IMM"];
    }
    else if ([mode isEqualToString:@"Read"])
    {
        [self sendCommand:@"*RST"];
    }
}

-(void)CloseDevice
{
    viClose (instr);
    status = viClose(findList);
    status = viClose (defaultRM);
    
    _opened = NO;
}

-(BOOL) sendCommand:(NSString*) cmd
{
    status = viSetAttribute (instr, VI_ATTR_TMO_VALUE, 8000);
    
    @autoreleasepool {
        NSString* dataLine = [[NSString alloc] initWithFormat:@"%@\n", cmd];
        NSInteger dataLen = [dataLine length];
        char inputBuffer[dataLen];
        strcpy(inputBuffer, [dataLine UTF8String]);
        
        status = viWrite (instr, (ViBuf)inputBuffer, (ViUInt32)strlen(inputBuffer), &writeCount);
        
        // [dataLine release];
        
        if (status < VI_SUCCESS)
        {
            return NO;
        }
        
        return YES;
    }
}

-(NSString*) readDataWithCountOfByte:(int)readDataCount
{
    unsigned char buffer[readDataCount];
    memset(buffer, 0, readDataCount);
    status = viRead (instr, buffer, readDataCount, &retCount);
    
    if (status < VI_SUCCESS)
    {
        return @"";
    }
    
    
    @autoreleasepool {
        
        NSString *result = [[NSString alloc] initWithUTF8String:(const char*)buffer];
        
        return result;
    }
}

-(NSString *) readDataByCommand:(NSString *)cmd
{
    [self sendCommand:cmd];
    NSString *value = [self readDataWithCountOfByte:1024];
    return value;
}

-(BOOL) Find
{
    status = viOpenDefaultRM (&defaultRM);
    
    if (status < VI_SUCCESS)
    {
        exit (EXIT_FAILURE);
    }
    status = viFindRsrc (defaultRM, /*"GPIB0::5::INSTR"*/"USB0::0x0957::0x0607::MY53018013::INSTR", &findList, &numInstrs, instrDescriptor);
    
    if (status < VI_SUCCESS)
    {
        printf ("An error occurred while finding resources.\n");
        fflush(stdin);
        //       getchar();
        viClose (defaultRM);
        return NO;
    }
    
    NSLog(@"%s",instrDescriptor);
    
    return YES;
}

@end
